package constant.milk.cashbook.select;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.AdapterView.OnItemClickListener;
import constant.milk.cashbook.R;
import constant.milk.cashbook.SuperActivity;
import constant.milk.cashbook.bean.CategoryMainData;
import constant.milk.cashbook.category.CategoryInsertActivity;
import constant.milk.cashbook.category.CategoryUpdateActivity;
import constant.milk.cashbook.database.DBSelectQuery;
import constant.milk.cashbook.util.MilkTextView;
import constant.milk.cashbook.util.MilkUtil;

public class SelectMainCategoryActivity extends SuperActivity
{
	private static final int INSERT_REQUEST_CODE = 0;
	private static final int UPDATE_REQUEST_CODE = 1;
	
	public static final String CATEGORY_MAIN_ALL_FLAG = "CATEGORY_MAIN_ALL_FLAG";
	public static final String CATEGORY_MAIN_LIST_DATA = "CATEGORY_MAIN_LIST_DATA";
	public static final String CATEGORY_MAIN_NAME_DATA = "CATEGORY_MAIN_NAME_DATA";

	private ListView listView;
	private Button checkButton;
	private Button upDeleteButton;
	private Button addButton;
	
	private MainCaregoryListAdapter listAdapter;
	
	private String items;
	
	private String id;
	
	private DBSelectQuery select;
	
	private RadioButton allRadio;
	
	private boolean allFlag;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.category_list_activity);
		
		setTitle("메인카테고리");
		
		setGoneTipButton();
		
		setFindViewId();
		setOnClick();
		setData();
	}

	@Override
	protected void setFindViewId()
	{
		listView = (ListView) findViewById(R.id.listView);
		
		checkButton = (Button) findViewById(R.id.checkButton);
		
		upDeleteButton = (Button) findViewById(R.id.upDeleteButton);
		
		addButton = (Button) findViewById(R.id.addButton);
	}

	@Override
	protected void setOnClick()
	{
		checkButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent();
				intent.putExtra(CATEGORY_MAIN_NAME_DATA, items);
				setResult(Activity.RESULT_OK, intent);
				finish();
			}
		});
		
		upDeleteButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (id.equals(""))
				{
					MilkUtil.showDialog(superContext, "알림", "선택된 메인카테고리가 하나도 없습니다.");
					return;
				}
				
				Intent intent = new Intent(superContext, CategoryUpdateActivity.class);
				intent.putExtra(CategoryUpdateActivity.MAIN_CATEGORY_ID, id);
				startActivityForResult(intent, UPDATE_REQUEST_CODE);
			}
		});
		
		addButton.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				Intent intent = new Intent(superContext, CategoryInsertActivity.class);
				startActivityForResult(intent, INSERT_REQUEST_CODE);
			}
		});
	}

	@Override
	protected void setData()
	{
		View view = getLayoutInflater().inflate(R.layout.radio_list_item, null);
		
		listView.addHeaderView(view, null, false);
		
		allRadio = (RadioButton) view.findViewById(R.id.radioItemRadio);
		((MilkTextView) view.findViewById(R.id.radioItemNameTextView)).setTextSize(TEXT_SIZE);
		((MilkTextView) view.findViewById(R.id.radioItemNameTextView)).setText(getString(R.string.all));
		view.setOnClickListener(new OnClickListener()
		{
			@Override
			public void onClick(View v)
			{
				if (!allRadio.isChecked())
				{
					items = "";
					allRadio.setChecked(true);
					listAdapter.notifyDataSetChanged();
				}
			}
		});
		
		select = new DBSelectQuery(superContext, superDb);
		
		Intent intent = getIntent();
		items = intent.getStringExtra(CATEGORY_MAIN_NAME_DATA);
		allFlag = intent.getBooleanExtra(CATEGORY_MAIN_ALL_FLAG, true);
		
		if (allFlag)
		{
			allRadio.setChecked(true);
			items = "";
		}
		else
		{
			allRadio.setChecked(false);
		}
		
		upDeleteButton.setVisibility(View.GONE);
		addButton.setVisibility(View.GONE);
		
		id = "";
		
		ArrayList<CategoryMainData> mainList = select.getCategoryMainList();
		
		listView.setOnItemClickListener(onItemClickListener);
		
		listAdapter = new MainCaregoryListAdapter(
				superContext, R.layout.radio_list_item, mainList);
		listView.setAdapter(listAdapter);
	}
	
	private OnItemClickListener onItemClickListener = new OnItemClickListener()
	{
		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int position,
				long arg3)
		{
			allRadio.setChecked(false);
			
			CategoryMainData data = (CategoryMainData) arg0.getItemAtPosition(position);
			id = data.getId();
			items = data.getName();
			listAdapter.notifyDataSetChanged();
		}
	};
	
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		if (resultCode != Activity.RESULT_OK)
		{
			return;
		}
		
		if (requestCode == INSERT_REQUEST_CODE)
		{
			ArrayList<CategoryMainData> mainList = select.getCategoryMainList();
			
			if (items.equals("") && mainList.size() > 0)
			{
				id = mainList.get(0).getId();
				items = mainList.get(0).getName();
			}
			
			listView.setOnItemClickListener(onItemClickListener);
			
			listAdapter = new MainCaregoryListAdapter(
					superContext, R.layout.radio_list_item, mainList);
			listView.setAdapter(listAdapter);
		}
		else if (requestCode == UPDATE_REQUEST_CODE)
		{
			ArrayList<CategoryMainData> mainList = select.getCategoryMainList();
			
			if (mainList.size() > 0)
			{
				for (int i = 0; i < mainList.size(); i++)
				{
					if (id.equals(mainList.get(i).getId()))
					{
						items = mainList.get(i).getName();
						break;
					}
					else
					{
						items = mainList.get(0).getName();
					}
				}
			}
			else
			{
				id = "";
				items = "";
			}
			
			listView.setOnItemClickListener(onItemClickListener);
			
			listAdapter = new MainCaregoryListAdapter(
					superContext, R.layout.radio_list_item, mainList);
			listView.setAdapter(listAdapter);
		}
	}
	
	@Override
	public void onBackPressed()
	{
		Intent intent = new Intent();
		intent.putExtra(CATEGORY_MAIN_NAME_DATA, items);
		setResult(Activity.RESULT_OK, intent);
		
		super.onBackPressed();
	}
	
	/**
	 * 리스트 아답터
	 */
	private class MainCaregoryListAdapter extends ArrayAdapter<CategoryMainData> 
	{
		public MainCaregoryListAdapter(Context context, int textViewResourceId, ArrayList<CategoryMainData> list) 
		{
			super(context, textViewResourceId, list);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) 
		{
			View v = convertView;
			
			if (v == null) 
			{
				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = inflater.inflate(R.layout.radio_list_item, null);
			}
			
			CategoryMainData data = getItem(position);
			
			MilkTextView nameTextView = (MilkTextView) v.findViewById(R.id.radioItemNameTextView);
			RadioButton radioButton = (RadioButton) v.findViewById(R.id.radioItemRadio);
			
			nameTextView.setTextSize(TEXT_SIZE);
			nameTextView.setText(data.getName());
			
			if (items.equals(data.getName()))
			{
				radioButton.setChecked(true);
				id = data.getId();
			}
			else
			{
				radioButton.setChecked(false);
			}
			
			return v;
		}
	}

	@Override
	protected void tipButton()
	{
	}

}
